//
//  LocalCache.m
//  VJSP
//
//  Created by 赵博 on 2017/9/25.
//  Copyright © 2017年 VJSP_LXB. All rights reserved.
//

#import "LocalCache.h"
#import <commoncrypto/CommonCrypto.h>
@implementation LocalCache

+ (id)cachePath:(NSString *)urlStr respose:(id)responseObject type:(NSInteger)type
{

    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *fileName = [self cachedFileNameForKey:urlStr];
    
    //2.找到cache文件夹在沙盒中得路径
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    
    NSString *dataFilePath = [cachePath stringByAppendingPathComponent:[self cachedFileNameForKey:@"LocalCache"]];
    if (![manager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil]) {
        [manager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
       
    }
    //3.路径文件名拼接
    NSString *cacheP = [dataFilePath stringByAppendingPathComponent:fileName];
    
    //4.把网络请求成功的数据（归档）
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:responseObject];
    //5.最后一步 写入本地
    if (0 == type) {
        //如果type是0则代表我要写入1读取
        [data writeToFile:cacheP atomically:YES];
        return nil;
    }else if (1 == type){
        //读取
        id data = [NSKeyedUnarchiver unarchiveObjectWithFile:cacheP]
        ;
        return data;
    }else{
        NSLog(@"%@", cacheP);
        return nil;
    }
    
    return cacheP;
}
+ (float)getPyqCacheSize{
    NSFileManager* manager =[NSFileManager defaultManager];
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *path = [cachePath stringByAppendingPathComponent:[self cachedFileNameForKey:@"LocalCache"]];
    if ([manager fileExistsAtPath:path]) {
        
        if ([manager attributesOfItemAtPath:path error:nil].fileSize > 1024 * 1024) {
            //GB
            return [manager attributesOfItemAtPath:path error:nil].fileSize / 1024.0f / 1024.0f;
        }else{
            //MB
            return [manager attributesOfItemAtPath:path error:nil].fileSize / 1024.0f;
        }
        
    }else{
        return 0.0f;
    }
    
    
}

+ (void)delePyqCache{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *dataFilePath = [cachePath stringByAppendingPathComponent:[self cachedFileNameForKey:@"LocalCache"]];
    BOOL blHave=[manager fileExistsAtPath:dataFilePath];
    if (blHave) {
        BOOL blDele= [manager removeItemAtPath:dataFilePath error:nil];
        if (blDele) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }else{
        NSLog(@"没有文件夹");
    }
    
    
    
}
// 利用MD5算法把网址转换成一串数字加字母
+ (NSString *)cachedFileNameForKey:(NSString *)key {
    const char *str = [key UTF8String];
    if (str == NULL) {
        str = "";
    }
    unsigned char r[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), r);
    NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                          r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
    return filename;
}
@end
